package clientInfo

import (
	"LRAdminServer/global"
	"LRAdminServer/model/clientInfo"
	clientInfoReq "LRAdminServer/model/clientInfo/request"
	"LRAdminServer/model/common/request"
	"LRAdminServer/model/common/response"
	"LRAdminServer/service"
	"LRAdminServer/utils"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type ClientPlatformApi struct {
}

var clientService = service.ServiceGroupApp.ClientInfoServiceGroup.ClientPlatformService

// CreateClientPlatform 创建登录平台
// @Tags ClientPlatform
// @Summary 创建登录平台
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body clientInfo.ClientPlatform true "创建登录平台"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"创建成功"}"
// @Router /client/createClientPlatform [post]
func (clientApi *ClientPlatformApi) CreateClientPlatform(c *gin.Context) {
	var client clientInfo.ClientPlatform
	err := c.ShouldBindJSON(&client)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	verify := utils.Rules{
		"AppId":    {utils.NotEmpty()},
		"Version":  {utils.NotEmpty()},
		"Platform": {utils.NotEmpty()},
		"Name":     {utils.NotEmpty()},
		"Enable":   {utils.NotEmpty()},
	}
	if err := utils.Verify(client, verify); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := clientService.CreateClientPlatform(&client); err != nil {
		global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败", c)
	} else {
		response.OkWithMessage("创建成功", c)
	}
}

// DeleteClientPlatform 删除登录平台
// @Tags ClientPlatform
// @Summary 删除登录平台
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body clientInfo.ClientPlatform true "删除登录平台"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
// @Router /client/deleteClientPlatform [delete]
func (clientApi *ClientPlatformApi) DeleteClientPlatform(c *gin.Context) {
	var client clientInfo.ClientPlatform
	err := c.ShouldBindJSON(&client)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := clientService.DeleteClientPlatform(client); err != nil {
		global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败", c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
}

// DeleteClientPlatformByIds 批量删除登录平台
// @Tags ClientPlatform
// @Summary 批量删除登录平台
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.IdsReq true "批量删除登录平台"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"批量删除成功"}"
// @Router /client/deleteClientPlatformByIds [delete]
func (clientApi *ClientPlatformApi) DeleteClientPlatformByIds(c *gin.Context) {
	var IDS request.IdsReq
	err := c.ShouldBindJSON(&IDS)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := clientService.DeleteClientPlatformByIds(IDS); err != nil {
		global.GVA_LOG.Error("批量删除失败!", zap.Error(err))
		response.FailWithMessage("批量删除失败", c)
	} else {
		response.OkWithMessage("批量删除成功", c)
	}
}

// UpdateClientPlatform 更新登录平台
// @Tags ClientPlatform
// @Summary 更新登录平台
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body clientInfo.ClientPlatform true "更新登录平台"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
// @Router /client/updateClientPlatform [put]
func (clientApi *ClientPlatformApi) UpdateClientPlatform(c *gin.Context) {
	var client clientInfo.ClientPlatform
	err := c.ShouldBindJSON(&client)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	verify := utils.Rules{
		"AppId":    {utils.NotEmpty()},
		"Version":  {utils.NotEmpty()},
		"Platform": {utils.NotEmpty()},
		"Name":     {utils.NotEmpty()},
		"Enable":   {utils.NotEmpty()},
	}
	if err := utils.Verify(client, verify); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := clientService.UpdateClientPlatform(client); err != nil {
		global.GVA_LOG.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败", c)
	} else {
		response.OkWithMessage("更新成功", c)
	}
}

// FindClientPlatform 用id查询登录平台
// @Tags ClientPlatform
// @Summary 用id查询登录平台
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query clientInfo.ClientPlatform true "用id查询登录平台"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
// @Router /client/findClientPlatform [get]
func (clientApi *ClientPlatformApi) FindClientPlatform(c *gin.Context) {
	var client clientInfo.ClientPlatform
	err := c.ShouldBindQuery(&client)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if reclient, err := clientService.GetClientPlatform(client.ID); err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败", c)
	} else {
		response.OkWithData(gin.H{"reclient": reclient}, c)
	}
}

// GetClientPlatformList 分页获取登录平台列表
// @Tags ClientPlatform
// @Summary 分页获取登录平台列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query clientInfoReq.ClientPlatformSearch true "分页获取登录平台列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /client/getClientPlatformList [get]
func (clientApi *ClientPlatformApi) GetClientPlatformList(c *gin.Context) {
	var pageInfo clientInfoReq.ClientPlatformSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if list, total, err := clientService.GetClientPlatformInfoList(pageInfo); err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(response.PageResult{
			List:     list,
			Total:    total,
			Page:     pageInfo.Page,
			PageSize: pageInfo.PageSize,
		}, "获取成功", c)
	}
}
